var API = require('API');
module.exports = ['$scope', function ($scope) {


    $scope.forms = {
        data:[],
        shop:{}
    };
    $scope.$watch('forms.shop', function(item){
        if (item.id) {
            users_shop_null();
            $scope.shop_data = item;
            shop_users(item.id);
            $scope.del_shop_id = item.id;
        }
        console.log($scope.shop_data);
    });


    //获取所有门店信息
    (function shop_all() {
        API.shop.all(function (data) {
            $scope.$apply(function () {
                $scope.forms.data = data;
            })
        })
    })();

    //获取没有门店的用户信息
    function users_shop_null() {
        API.shop_user.users_without_shop(function (data) {
            $scope.$apply(function () {
                $scope.users_null_shop = data;
            })
        });
    }

    //每个门店的用户
    function shop_users(value) {
        API.shop_user.users_of_shop(value, function (data) {
            $scope.$apply(function () {
                $scope.shop_users_list = data;
            })
        });
    }

    //每个门店的点击事件
    // $scope.shop_all_list = function (list, data) {
    //     if (list.iscolor) {
    //         list.iscolor = false;
    //     } else {
    //         for (var i = 0; i < data.length; i++) {
    //             $scope.shop_all_data[i].iscolor = false;
    //         }
    //         list.iscolor = true;
    //     }
    //     users_shop_null();
    //     $scope.shop_data = list;
    //     shop_users(list.id);
    //     $scope.del_shop_id = list.id;
    // };

    //add shop
    $scope.add_shop = function (list) {
        API.shop_user.add_user_to_shop(list, $scope.shop_data, function(){
            users_shop_null();
            shop_users($scope.del_shop_id);
        });
    };

    $scope.del_user_shop = function (list) {
        API.shop_user.remove_user_from_shop(list, function(){
            users_shop_null();
            shop_users($scope.del_shop_id);
        });
    }
}];